package www.ym.com.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import www.ym.com.entity.AccountLog;
import www.ym.com.entity.Consum;
import www.ym.com.mapper.AccountLogMapper;
import www.ym.com.mapper.AccountMapper;
import www.ym.com.service.AccountLogService;

import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
@Service
public class AccountLogServiceImpl extends ServiceImpl<AccountLogMapper, AccountLog> implements AccountLogService {

    @Autowired
    private AccountLogMapper accountLogMapper;

    @Override
    public Page<AccountLog> logList(JSONObject jsonObject) {
        //当前页数
        Long pageNum = Long.valueOf(String.valueOf(jsonObject.get("pageNum")));
        //当前页有多少条数据
        Long pageSize = Long.valueOf((String.valueOf(jsonObject.get("pageSize"))));

        //用户useId
        Long useId = Long.valueOf((String.valueOf(jsonObject.get("userId"))));

        //银行卡id
        Long bankId = Long.valueOf((String.valueOf(jsonObject.get("bankId"))));

        String type = String.valueOf(jsonObject.get("type"));

        String startTime = String.valueOf(jsonObject.get("startTime"));
        String endTime = String.valueOf(jsonObject.get("endTime"));
        log.info("startTime" + startTime);
        log.info("endTime" + endTime);

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        //交易明细区间
        String startTime1 = "";
        String endTime1 = "";

        if (!startTime.isEmpty()) {
            startTime1 = dateFormat.format(new Date(Long.valueOf(startTime)));
        }
        if (!endTime.isEmpty()) {
            endTime1 = dateFormat.format(new Date(Long.valueOf(endTime)));
        }

        //筛选条件
        LambdaQueryWrapper<AccountLog> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AccountLog::getBankId, bankId).eq(AccountLog::getUserId, useId)
                .eq(type != "", AccountLog::getType, type)
                .ge(startTime != "", AccountLog::getCreateTime, startTime1)
                .le(endTime != "", AccountLog::getCreateTime, endTime1);

        Page<AccountLog> userPage = accountLogMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
//        System.out.println("数据为：" + userPage.getRecords());
//        System.out.println("总数为：" + userPage.getTotal() + ",总页数为：" + urf.getPages());
//        System.out.println("当前页为：" + userPage.getCurrent() + ",每页限制：" + urf.getSize());

        return userPage;
    }
}
